package leetcode_递归;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 电话号码的字母组合
 * 算法：
 *   递归
 *   先创建一个数字与字母对应的哈希表
 */
public class _17 {
    public static void main(String[] args) {
        System.out.println(new _17().letterCombinations("234"));
        System.out.println((short)'\u8000');
    }

    public List<String> letterCombinations(String digits) {
        List<String> ans = new ArrayList<>();
        if (digits.equals("")) {
            return ans;
        }
        HashMap<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");
        StringBuilder tem = new StringBuilder();
        int x = 0;
        dfs(map, digits, x, ans, tem);
        return ans;
    }
    public void dfs(HashMap<Character, String> map, String digits, int x, List<String> ans, StringBuilder tem) {
        if (tem.length() == digits.length()) {
            ans.add(tem.toString());
            return;
        }
        String cur = map.get(digits.charAt(x));
        for (int i = 0; i < cur.length(); i++) {
            tem.append(cur.charAt(i));
            dfs(map, digits, x + 1, ans, tem);
            tem.deleteCharAt(tem.length() - 1);
        }
    }
}
